/**
Author: udayan
*/

package com.lightonyoga;
import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


public class ShowWeekDetails extends Activity {
	Context context;
	String TAG = "LightOnYoga: ShowWeekDetails";
	ArrayList<String> lv_arr;
	private static ListView lvmain;
	Week week;
	ArrayList<String> aasan;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this.getApplicationContext();
        Bundle bundle = this.getIntent().getExtras();
        
        Log.i(TAG,"Oncreate");
        View header = getLayoutInflater().inflate(R.layout.headerlayoutweekview, null);

        lvmain = (ListView)  findViewById(R.id.listViewMain);
		lvmain.setClickable(true);

        lvmain.addHeaderView(header, null, false);
		
		//builder.setMessage("Are you sure?").setPositiveButton("Okay", dialogClickListener);
        week = (Week) bundle.getSerializable("week"); 
        if(week == null) {
        	Log.i(TAG,"week parameter received was null");
        }
        //show the weeks 
        makeDisplayReady();
    } 

    void makeDisplayReady () {
    	//load the list view
    	loadAasan();
    	//displayWeeks
    	displayAasan();
    	//add a event listener for list view clicks.
    	attachEventListenerToList();
    }
    void loadAasan() {
    	aasan = week.getAasan();
    	//week.print();
    }
    
    void displayAasan() {
    	TextView tv =  (TextView)  findViewById(R.id.headerweeklistitem);
    	tv.setText("Week Numer : " + week.getWeekNum());
    	lv_arr = new ArrayList<String>();
    	for (int i=0; i<aasan.size();i++) {
    		lv_arr.add(aasan.get(i));
    	}
    	lvmain.setAdapter(new ListAdapterWeek(this, R.layout.list_view_week,lv_arr));
    }
    
    
	void attachEventListenerToList() {
		//starts another activity to show the detail results for a Device
		lvmain.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
				  //week is in format week xxx
	    		  String aasan = ((String)lvmain.getItemAtPosition(pos));
	    		  Log.i(TAG, "OnItemClickListener you clicked position " + pos +" and the contect was "+ lvmain.getItemAtPosition(pos) + "  and aasan is "+ aasan);
	    		  Bundle bundle = new Bundle();
	    		  if(LightOnYogaActivity.aasans == null) {
	    			  //load the aasans now 
	    			  LightOnYogaActivity.loadAasans(); 
	    		  }
	    		
	    		  bundle.putSerializable("aasan", aasan);
	    		  Intent newIntent = new Intent(context, ShowAasanDetails.class);
	    		  newIntent.putExtras(bundle);
	    		  startActivityForResult(newIntent,0);
	    		 
			}
		}) ;
	}

    
}
